package com.orphan.marketFront.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.orphan.marketFront.mapper.UserDetailMapper;
import com.orphan.marketFront.po.UserDetail;
import com.orphan.marketFront.service.IUserDetailService;
import com.orphan.util.dto.UserDto;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lucangtian
 * @since 2021-07-16
 */
@Service
public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetail> implements IUserDetailService {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    UserDetailMapper userDetailMapper;

    @Override
    public int save(Long uId, String company, String identity, String license) {
        try {
            return userDetailMapper.save(uId, company, identity, license);
        }catch (Exception e){
            return 0;
        }
    }

    /**
     * 回调方法
     */
    RabbitTemplate.ConfirmCallback callback=(CorrelationData correlationData, boolean b, String s)->{
        //投递成功
        if(b){
            //获得关联数据
            String userIdentity=correlationData.getId();
            System.out.println("投递成功");
        }
    };

    public void sendUser(UserDto userDto) {
        //开启手动确认
        rabbitTemplate.setMandatory(true);//开启消息确认
        //绑定回调函数
        rabbitTemplate.setConfirmCallback(callback);

        //组装关联数据
        String identity = userDto.getIdentity();
        CorrelationData correlationData=new CorrelationData(identity);
        //投递
        rabbitTemplate.convertAndSend("userDetailExchange","userDetailKey",userDto,correlationData);

    }


//    @RabbitListener(queues = "userDetailQueue")
//    public void getMQMessage(UserDto userDto, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
//
//        System.out.println(userDto);
//
//        //手动确认  false--不批量确认
//        channel.basicAck(tag,false);
//
//    }

//    @RabbitListener(queues = "")
//    public void getMQMessage(UserDto userDto, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
//
//        System.out.println(userDto);
//        //修改数据库
//
//        //手动确认  false--不批量确认
//        channel.basicAck(tag,false);
//
//    }
}
